﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace BITUI.Audio
{
    class AudioData_Mp3 : IAudioData
    {
        public string[] ExtNames => new string[] { ".mp3" };
        public int Freq => Mp3Data.Frequency;

        public int Channel => Mp3Data.Channel;//這個stream讀取器把一切都變成了2channel  Mp3Stream.ChannelCount;

        public float Time => Mp3Data.ReadTimer;

        public float PlayTime
        {
            get => throw new NotSupportedException();
            set => throw new NotSupportedException();

        }

        public void Dispose()
        {
            Mp3Data.Dispose();
        }
        MP3SharpFix.Mp3Data Mp3Data;
        MP3SharpFix.Mp3StreamDecoder Mp3Decoder;
        public bool Load(string filename)
        {
            var stream = System.IO.File.OpenRead(filename);
            return Load(stream);
        }

        public bool Load(Stream stream)
        {
            Random r = new Random();

            //bitstream test
            //for (var k = 0; k < 100; k++)
            //    for (var i = 0; i < 18; i++)
            //    {
            //        var bys = new byte[8];
            //        r.NextBytes(bys);
            //        //bys[0] = 0xf0;
            //        //bys[1] = 0xcc;
            //        //bys[2] = 0x77;
            //        //bys[3] = 0x00;
            //        //bys[4] = 0xf0;
            //        //bys[5] = 0xcc;
            //        //bys[6] = 0x77;
            //        //bys[7] = 0x00;
            //        var ms = new System.IO.MemoryStream(bys);
            //        var bits = new MP3Sharp.Decoding.Bitstream(new MP3Sharp.Decoding.PushbackStream(ms, 8));
            //        bits.read_frame_data(8);
            //        bits.ParseFrame();

            //        var bitsmy = new MP3SharpFix.BitStreamClean(bys);
            //        bitsmy.GetBitsFromBuffer(16);
            //        bits.GetBitsFromBuffer(16);
            //        bitsmy.GetBitsFromBuffer(13);
            //        bits.GetBitsFromBuffer(13);
            //        var resultmy = bitsmy.GetBitsFromBuffer(i);
            //        var result = bits.GetBitsFromBuffer(i);
            //        if (result != resultmy)
            //        {
            //            Console.WriteLine("fail calc.");
            //        }
            //        Console.WriteLine("bitresult from 0xF0 CC 77 00  i= " + i + " v=" + result.ToString("X08")
            //            + " vmy=" + resultmy.ToString("X08"));
            //    }
            //终于改进了mp3Stream的读取方法
            byte[] buf = new byte[stream.Length];
            stream.Read(buf, 0, buf.Length);
            stream.Position = 0;

            Mp3Data = new MP3SharpFix.Mp3Data(stream);
            Mp3Decoder = new MP3SharpFix.Mp3StreamDecoder(Mp3Data);
            //第二步改进解码器，首先是BitStream 去掉PushbackStream这种奇怪的东西
            //var time = data.TotalTimer;


            return true;
        }

        public int ReadFloat(float[] data, int offset, int count)
        {
            return Mp3Decoder.ReadFloat(data, offset, count);

        }
    }

}
